<?php
namespace Admin\Controller;
use Think\Controller;
class CommonController extends Controller {
    protected $controllerName = CONTROLLER_NAME;
    protected $actionName = ACTION_NAME;
    protected $uid = ACTION_NAME;

	function _initialize() {
		header("Content-Type:text/html;charset=utf-8");
        header('Content-Type:application/json; charset=utf-8');
        defined('IN_ADMIN') or exit('Access Denied');

        $this->checkLogin();
        $this->auth();
        $this->uid = session('user_id');

        $this->assign('menuList',$this->getMenuList());


        $menu_pid = I('menu_pid');
        if(empty($menu_pid)){
            $Menu = D('Menu');
            $menu_pid = $Menu->getPid(strtolower($this->controllerName),strtolower($this->actionName));
        }
        $this->assign("menu", $this->show_menu($menu_pid));
        $this->assign("sub_menu", $this->show_sub_menu($menu_pid));


        $Admin = M('Admin');
        $adminInfo = $Admin->where( array('id'=>$this->uid) )->find();
        $this->assign('adminInfo',$adminInfo);

        //后台左菜单二维码
        $this->assign('QRcodeUrl','/Public/images/liqingbo_qrcode.png');
    }

    function checkLogin(){
    	if(!session('user_id')){
    		$this->display('Login:login');
    		exit;
    	}
    }

    function auth(){
        $uid = session('user_id');

        $node = strtolower(CONTROLLER_NAME.'-'.ACTION_NAME);
        $node_array = array(
            'statis-index',
            'statis-column',
            'course-index',
            'source-index',
            'partner-index',
            'classify-level',
            'classify-rank',
            'menu-index',
            'node-index',
            'admin-index',
            'admin-add',
            'admin-del',
            'role-index',
            'role-add'
            );
        if($uid!=1){
            if(in_array($node, $node_array)){
                echo '没有权限';
                exit;
            }
        }
    }

    /**
     * 显示一级菜单
     */
    private function show_menu($menu_pid=0) {
        $css = '';
        $menu_list = get_admin_menu_tree();

        $count = count($menu_list);
        $i = 1;
        $menu = "";

        foreach ($menu_list as $key => $val) {

            if ($i == 1) {
                $css = ($val['id'] == $menu_pid) ? "fisrt_current" : "fisrt";
                $menu .= '<li class="'.$css.'"><span><a href="'.U($val['controller'].'/'.$val['action']).'">' . $val['name'] . '</a></span></li>';
            } else if ($i == $count) {
                $css = ($val['id'] == $menu_pid) ? "end_current" : "end";
                $menu .= '<li class="' . $css . '"><span><a href="' . U($val['controller'].'/'.$val['action']).'">' . $val['name'] . '</a></span></li>';
            } else {
                $css = $val['id'] == $menu_pid ? "current" : "";
                $menu .= '<li class="' . $css . '"><span><a href="' . U($val['controller'].'/'.$val['action']). '">' . $val['name'] . '</a></span></li>';
            }
            $i++;
        }
        return $menu;
    }

    /**
     * 显示二级菜单
     */
    private function show_sub_menu($menu_pid=0) {
        $menu_list = get_admin_menu_tree();
        $secondary = $menu_list[$menu_pid];
        if(!empty($secondary)){
            foreach ($secondary['sub'] as $key => $val) {
                if(strtolower($val['action'])==strtolower($this->actionName)&&strtolower($val['controller'])==strtolower($this->controllerName)){
                    $secondary['sub'][$key]['class'] = 'on';
                }else{
                    $secondary['sub'][$key]['class'] = '';
                }
            }
        }

        return $secondary;
    }

    public function getMenuList() {
        $M = M('Menu');
        $role_id = $this->roleId;
        $map['status'] = 1;
        if($role_id!=1){
            $map['group'] = array('like','%'.$role_id.'%');
        }
        $list = $M->where($map)->order('pid ASC,sort ASC')->select();
        foreach($list as $key=>$val){
            if($val['pid']==0){
                $menuList[$val['id']] = $val;
            }else{
                $menuList[$val['pid']]['sub'][] = $val;
            }
        }
        return $menuList;
    }

}